package clientView;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

import object.Position;
import utility.StringUtil;
import clientMain.Main;
import clientUtilGUI.UtilityGUI;
import database.PositionDelegate;

public class Pos_Update  extends JDialog implements ActionListener {

	private JTextField txtPosName;
	private JLabel lblPosTName;
	private JLabel lblPosDes;
	private JTextArea txaPosDes;
	private JButton btnSave;
	private JButton btnCancel;
	private JScrollPane scrollPane;
	private int rowCount;
    private String name;
    private String des;
    private int posId;
    boolean flag  = false;
	/**
	 * Create the frame.
	 */

	public Pos_Update(JFrame main, String posName,String posDes,final int id,int row)  {
	  
		super(main, "Sửa Bảng Chức Vụ", true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		rowCount = row;
		name = posName;
		des = posDes;
		posId = id;
		int height = 200;
	    int width = 420;
	    setSize(width, height);
	    setLocation((UtilityGUI.screenSize.width-width)/2, (UtilityGUI.screenSize.height-height)/2);
		setResizable(false);
	    initComponet();
	}

		public void initComponet(){
	    GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0};
		getContentPane().setLayout(gridBagLayout);
		
		lblPosTName = new JLabel("Chức Vụ: ");
		lblPosTName.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblPosTName = new GridBagConstraints();
		gbc_lblPosTName.anchor = GridBagConstraints.WEST;
		gbc_lblPosTName.insets = new Insets(15, 15, 5, 5);
		gbc_lblPosTName.fill = GridBagConstraints.VERTICAL;
		gbc_lblPosTName.gridx = 0;
		gbc_lblPosTName.gridy = 0;
		getContentPane().add(lblPosTName, gbc_lblPosTName);
		
		txtPosName = new JTextField();
		txtPosName.setBackground(UtilityGUI.colorBgOldTxtUpdate);
		txtPosName.setForeground(UtilityGUI.colorOldTxtUpdate);
		txtPosName.setFont(UtilityGUI.fontTxtInsert);
		txtPosName.setText(name);
		txtPosName.addFocusListener(new FocusAdapter() {
			public void focusLost(FocusEvent e){
				txtPosName.setText(StringUtil.removeUnwantedSpace(txtPosName.getText()));
			}
		});
		txtPosName.addFocusListener(new FocusListener() {
			
			@Override
			public void focusLost(FocusEvent e) {
				if(txtPosName.getText().length()==0 || txtPosName.getText() == null){
					txtPosName.setText(name);
					txtPosName.setBackground(UtilityGUI.colorBgOldTxtUpdate);
					txtPosName.setForeground(UtilityGUI.colorOldTxtUpdate);
				}
			}			
			@Override
			public void focusGained(FocusEvent e) {
				if(txtPosName.getText().equals(name)){
				txtPosName.setBackground(UtilityGUI.colorBgNewTxtUpdate);
				txtPosName.setFont(UtilityGUI.fontTxtInsert);
				txtPosName.setText("");
				}
			}
		});
		//check database 
		txtPosName.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent arg0) {
				PositionDelegate d = new PositionDelegate();
				if(!d.CheckInsertPosName((StringUtil.removeUnwantedSpace(txtPosName.getText())))&&txtPosName.getText().length()>3){
					btnSave.setEnabled(true);
				}else{
					btnSave.setEnabled(false);
					
				}
			
			}
		});
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.gridwidth = 2;
		gbc_textField.insets = new Insets(15, 5, 5, 15);
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 1;
		gbc_textField.gridy = 0;
		getContentPane().add(txtPosName, gbc_textField);
		txtPosName.setColumns(10);
		
		
		lblPosDes = new JLabel("Mô tả: ");
		lblPosDes.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblEmTDes = new GridBagConstraints();
		gbc_lblEmTDes.anchor = GridBagConstraints.NORTHWEST;
		gbc_lblEmTDes.insets = new Insets(5, 15, 5, 5);
		gbc_lblEmTDes.gridx = 0;
		gbc_lblEmTDes.gridy = 1;
		getContentPane().add(lblPosDes, gbc_lblEmTDes);
		
		txaPosDes = new JTextArea(4,10);
		txaPosDes.setBackground(UtilityGUI.colorBgOldTxtUpdate);
		txaPosDes.setForeground(UtilityGUI.colorOldTxtUpdate);
		txaPosDes.setFont(UtilityGUI.fontTxtInsert);
		txaPosDes.append(des);

		//Event
		txaPosDes.addFocusListener(new FocusListener() {
			boolean flag = false;
			@Override
			public void focusLost(FocusEvent e) {
				String a =  "  ";
				a = a.trim();
				if(txaPosDes.getText().trim().equals(a)){
					txaPosDes.setText(des);
					txaPosDes.setBackground(UtilityGUI.colorBgOldTxtUpdate);
					txaPosDes.setForeground(UtilityGUI.colorOldTxtUpdate);
					flag = false;
				}else if(!txaPosDes.getText().equals(des)){
					flag = true;
				}
			}
			
			@Override
			public void focusGained(FocusEvent e) {
				if(flag  == false){
					txaPosDes.setText("");
					txaPosDes.setBackground(UtilityGUI.colorBgNewTxtUpdate);
					txaPosDes.setForeground(UtilityGUI.colorNewTxtUpdate);
				}
			}
		});
		scrollPane = new JScrollPane(txaPosDes);
		GridBagConstraints gbc_textArea = new GridBagConstraints();
		gbc_textArea.gridwidth = 2;
		gbc_textArea.insets = new Insets(5, 5, 5, 15);
		gbc_textArea.fill = GridBagConstraints.BOTH;
		gbc_textArea.gridx = 1;
		gbc_textArea.gridy = 1;
		getContentPane().add(scrollPane, gbc_textArea);
		
		btnSave = new JButton("Lưu");
		btnSave.setEnabled(false);
		btnSave.setFont(UtilityGUI.fontBtnForm);
		GridBagConstraints gbc_btnSave = new GridBagConstraints();
		gbc_btnSave.fill = GridBagConstraints.VERTICAL;
		gbc_btnSave.anchor = GridBagConstraints.EAST;
		gbc_btnSave.insets = new Insets(10, 0, 5, 5);
		gbc_btnSave.gridx = 1;
		gbc_btnSave.gridy = 2;
		getContentPane().add(btnSave, gbc_btnSave);
		btnSave.addActionListener(this);
		
		btnCancel = new JButton("Thoát");
		btnCancel.setFont(UtilityGUI.fontBtnForm);
		GridBagConstraints gbc_btnCancel = new GridBagConstraints();
		gbc_btnCancel.fill = GridBagConstraints.BOTH;
		gbc_btnCancel.insets = new Insets(10, 0, 5, 15);
		gbc_btnCancel.gridx = 2;
		gbc_btnCancel.gridy = 2;
		getContentPane().add(btnCancel, gbc_btnCancel);
		btnCancel.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnSave){
			Position pos = new Position();
			pos.setPosId(posId);
			pos.setPosName(StringUtil.removeUnwantedSpace(txtPosName.getText()));
			pos.setPosDes(StringUtil.removeUnwantedSpace(txaPosDes.getText()));
			PositionDelegate  p = new PositionDelegate();
			boolean flag = p.updatePos(pos, Main.userId);
			if(flag == true){
				this.setVisible(false);
				JOptionPane.showMessageDialog(null, UtilityGUI.successAction, 
							UtilityGUI.titleAlert, JOptionPane.INFORMATION_MESSAGE);
				Pos_View.tblPos.getModel().setValueAt(StringUtil.removeUnwantedSpace(txtPosName.getText()), rowCount, 1) ;
				Pos_View.tblPos.getModel().setValueAt(StringUtil.removeUnwantedSpace(txaPosDes.getText()), rowCount, 2) ;
				 
			}else{
				JOptionPane.showMessageDialog(null, UtilityGUI.errorAction ,
						UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
			}
			
		}else if(e.getSource() == btnCancel){
			dispose();
		}
	}
		
}
